<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="tmplContentForm" slot-scope="{type}">
       
       <div v-if="type='edit'||'add'">
        <el-input
           type="textarea"
           placeholder="请输入内容"
           v-model="form.tmplContent"
           maxlength="140"
           show-word-limit
        />
        <div class="title">提示以下为变量字段：${title} 商品名称, ${coupon} 券码, ${expiredate} 过期时间, ${cardNo} 卡号, ${amount} 金额,${chargeTime}充值时间, ${merchant} 店铺名称, ${balance} 余额, ${count} 数量</div>
       </div>
      </template>
      <template slot="menuLeft">
        <!-- <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.template_delete"
                   @click="handleDelete">删 除
        </el-button> -->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
} from "@/api/sms-bmg/template";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: false,
        viewBtn: true,
        selection: false,
        dialogClickModal: false,
        addBtnText: "创建短信模版",
        addTitle: "创建短信模版",
        editTitle: "编辑短信模版",
        labelWidth: 130,
        column: [
          {
            label: "短信编码",
            prop: "id",
            addDisplay: false,
            editDisplay: false,
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入短信编码",
                trigger: "blur",
              },
            ],
          },
          {
            type: "select",
            label: "选择短信供应商",
            span: 14,
            prop: "prvdIdList",
            align: "center",
            multiple: true,
            hide: true,
            tip:'按照排序设置优先级',
            tipPlacement: 'top',
            dicUrl: "/api/sms-bmg/provider/all",
            dicMethod: "get",
            props: {
              label: "prvdName",
              value: "id",
              res: "data",
            },
            dataType: "string",
            rules: [
              {
                required: true,
                message: "请选择短信供应商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "模板标题",
            span: 14,
            prop: "tmplName",
            align: "center",
            search: true,
            tip:'格式建议：业务线_归属部门_场景',
            tipPlacement: 'top',
            rules: [
              {
                required: true,
                message: "请输入模板标题",
                trigger: "blur",
              },
              {
                max: 30,
                message: "文字超出限制",
                trigger: "blur",
              },
            ],
          },
          {
            label: "短信签名",
            span: 14,
            prop: "signName",
            align: "center",
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入短信签名",
                trigger: "blur",
              },
               {
                max: 8,
                message: "文字超出限制",
                trigger: "blur",
              },
            ],
          },

          {
            label: "模版内容",
            prop: "tmplContent",
            type: "textarea",
            span: 16,
            align: "center",
            rules: [
              {
                required: true,
                message: "请输入短信内容",
                trigger: "blur",
              },
              {
                max: 140,
                message: "文字超出限制",
                trigger: "blur",
              },
            ],
          },
          {
            label: "更新时间",
            prop: "updateTime",
            align: "center",
            addDisplay: false,
            editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入更新时间",
                trigger: "blur",
              },
            ],
          },
          {
            label: "操作人",
            prop: "updateUserName",
            align: "center",
            addDisplay: false,
            editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入操作人",
                trigger: "blur",
              },
            ],
          },
          {
            label: "所属部门",
            prop: "updateDeptName",
            align: "center",
            addDisplay: false,
            editDisplay: false,
            rules: [
              {
                required: true,
                message: "请输入所属部门",
                trigger: "blur",
              },
            ],
          },
          {
            type: "radio",
            label: "状态",
            align: "center",
            value: "1",
            span: 10,
            prop: "status",
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=SMS_TEMPLATE_STATUS",
            dicMethod: "get",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请选择状态",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.template_add, true),
        viewBtn: this.vaildData(false),
        delBtn: this.vaildData(false),
        editBtn: this.vaildData(this.permission.template_edit, true),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    rowSave(row, done, loading) {
      row.prvdIdList = row.prvdIdList.split(",");
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      row.prvdIdList = row.prvdIdList.split(",");
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      // this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
  },
};
</script>

<style>
.title {
  /* margin-left: 10px; */
  /* border:1px solid red; */
  color:red;
  width:100%
}
</style>
